// 登录函数
var login = function(){
	var loginData = {
		username:'',
		password:''
	}
	// 手机号验证正则
	var unreg = /1[3-9]{1}[0-9]{9}/;
	loginData.username = $('#username').val();
	loginData.password = $('#password').val();
	if(!unreg.test(loginData.username)){
		layer.msg("您输入的手机号码有误，请重新输入")
		return;
	}
	if (!loginData.password || loginData.password.length < 6 || loginData.password.length > 20) {
		layer.msg("您输入的密码有误，请重新输入")
		return;
	}
	layer.load(1, {
	    shade: [0.6,'#000'] 
	});
	
	$.ajax({
	    url:ajaxURL+'/api/user/Login',
	    type:'POST',  
	    async: false,   
	    data:loginData,  
	    contentType: "application/x-www-form-urlencoded",
	    success:function(result){
	    	layer.closeAll('loading');
	    	if(result.code == 1){
                if(result.data.userInfo.userType == 3){
                    var html = '<div id="js_singlePrice">';
                    html += '       <section class="withtan_box">';
                    html += '           <p class="witht_b_tit text-center">';
                    html += '               <span>&nbsp;提示</span>';
                    html += '           </p>';
                    html += '           <div class="withtan_content">';
                    html += '               <div class="with_c_div">';
                    html += '                   尊敬的用户：<br/><br/>';
                    html += '                   为了给您带来更优质的服务及体验，有利金服已于2018年2月4日完成系统升级。升级后系统变为双重资金监管模式。由于您的账户内有尚未投资过的金额，请您联系客服办理提现事宜。<br/><br/>';
                    html += '                   客服电话：400-600-3538<br/>';
                    html += '                   电话客服时间：9:00-18:00<br/>';
                    html += '                   微信搜索【有利金服】公众号，关注后即可在线联系客服<br/>';
                    html += '                   微信客服时间：9:00-22:00<br/>';
                    html += '               </div>';
                    html += '           </div>';
                    html += '       </section>';
                    html += '   </div>';
                    layer.open({
                    	// area: ['500px', '400px'],
						type: 1,
						shade: [0.2,'#000'],
						title: false,
						content: html,
						closeBtn: 0,
						shadeClose: false
					});
                } else if(result.data.userInfo.userType == 1){
                    layer.msg('虚拟人不能登录')
                } else{
                    localStorage.setItem('uid',result.data.gid)
		    		localStorage.setItem('userinfo',JSON.stringify(result.data.userInfo))
		    		setTimeout(function(){
		    			window.location.pathname = "/recharge/index.html"
			    	},50)
                }
	    		
	    	}else if(result.code == 1083){//同盾
	    		var html = '';
				html += '<div class="td_bg">';
				html += '     <span class="td_close">';
				html += '    </span>';
				html += '</div>';
				layer.open({
					type: 1,
					shade: [0.2,'#000'],
					title: false,
					content: html
				});
				var Tdparent = document.querySelector('.td_bg');
				var Tdclose = document.querySelector('.td_close');
				Tdparent.parentNode.parentNode.style.background = "rgba(255,255,255,0)";
				Tdparent.parentNode.parentNode.style.boxShadow = 'none'
				Tdclose.onclick = function () {
					layer.closeAll();
				}
	    	}else if(result.data){
	    		layer.msg(result.data)
	    	}else{
	    		layer.msg('登录失败，请稍后再试')
	    	}
	    },
	    error:function(error){
	    	setTimeout(function(){
	    		layer.closeAll('loading');
	    	},500)
	    }
	})
}
// 点击登录
$('#loginin').on("click",login)
// 密码输入框回车键登录
$('#password').on("keypress",function (e) {
    if (e.keyCode == 13) {
        login();
    }
})
// 手机号输入框监听
$('#username').on("focus",function (e) {
	layer.closeAll()
})
// 密码输入框监听
$('#password').on("focus",function (e) {
	layer.closeAll()
})